package com.trade.dao.impl;

import java.util.Collections;
import java.util.List;

import com.trade.common.dao.impl.GenericDao;
import com.trade.dao.IProcurmentContractDao;
import com.trade.exception.PersistenceException;
import com.trade.model.ProcurmentContract;

/**
 * @author Lin Yang
 * 
 */
public class ProcurmentContractDaoImpl extends GenericDao<ProcurmentContract, Long> implements IProcurmentContractDao {

    @Override
    public List<ProcurmentContract> findByStateAndLikeName(int state, String name) throws PersistenceException {
        try {
            List<ProcurmentContract> contracts = null;
            if (name == null || name.trim().isEmpty()) {
                contracts = find("from ProcurmentContract where state=?", state);
            } else {
                contracts = find("from ProcurmentContract where state=? and name like ?", new Object[] { state, "%" + name.trim() + "%" });
            }

            if (contracts == null || contracts.size() == 0) {
                return Collections.emptyList();
            }

            return contracts;
        } catch (Exception e) {
            throw new PersistenceException("查询采购合同失败", e);
        }
    }

    @Override
    public List<ProcurmentContract> findLikeName(String name) throws PersistenceException {
        try {
            List<ProcurmentContract> contracts = null;
            if (name == null || name.trim().isEmpty()) {
                contracts = find("from ProcurmentContract");
            } else {
                contracts = find("from ProcurmentContract where name like ?", "%" + name.trim() + "%");
            }

            if (contracts == null || contracts.size() == 0) {
                return Collections.emptyList();
            }

            return contracts;
        } catch (Exception e) {
            throw new PersistenceException("查询采购合同失败", e);
        }
    }

}
